package com.example.customview

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.widget.Scroller

/**
 * @author by liliang on 2020/5/30 12:31 PM
 */
class CustomView : View {

    private var lastX: Int = 0
    private var lastY: Int = 0
    private lateinit var scroller: Scroller


    constructor(context: Context, attributeSet: AttributeSet, styleAttr: Int) : super(
        context,
        attributeSet,
        styleAttr
    )

    constructor(context: Context, attributeSet: AttributeSet): super(context, attributeSet) {
        this.scroller = Scroller(context)
    }
    constructor(context: Context) : super(context)

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        val x = event.x.toInt()
        val y = event.y.toInt()
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                lastX = x
                lastY = y
            }
            MotionEvent.ACTION_MOVE -> {
                val offsetX = x - lastX
                val offsetY = y - lastY
                layout(left + offsetX, top + offsetY, right + offsetX, bottom + offsetY)
//                offsetLeftAndRight(offsetX)
//                offsetTopAndBottom(offsetY)
//                val apply = (layoutParams as LinearLayout.LayoutParams).apply {
//                    leftMargin = left + offsetX
//                    topMargin = top + offsetY
//                }
//                val apply = (layoutParams as ViewGroup.MarginLayoutParams).apply {
//                    leftMargin = left + offsetX
//                    topMargin = top + offsetY
//                }
//                layoutParams = apply
                //调用layout方法来重新放置它的位置
//                layout(getLeft()+offsetX, getTop()+offsetY,
//                       getRight()+offsetX , getBottom()+offsetY);

                //对left和right进行偏移
//                offsetLeftAndRight(offsetX);
                //对top和bottom进行偏移
//                offsetTopAndBottom(offsetY);
                //使用LayoutParams
//                LinearLayout.LayoutParams layoutParams= (LinearLayout.LayoutParams) getLayoutParams();
//                layoutParams.leftMargin = getLeft() + offsetX;
//                layoutParams.topMargin = getTop() + offsetY;
//                setLayoutParams(layoutParams);

                //使用MarginLayoutParams
//                ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();
//                layoutParams.leftMargin = getLeft() + offsetX;
//                layoutParams.topMargin = getTop() + offsetY;
//                setLayoutParams(layoutParams);
                //使用scrollBy

//                (parent as View).scrollBy(-offsetX, -offsetY)


            }
        }
        return true
    }

    fun smoothScrollTo(destX: Int, destY: Int) {
        val scrollX = scrollX
        val delta = destX - scrollX
        //1000秒内滑向destX
        scroller.startScroll(scrollX, 0, delta, 0, 2000)
        invalidate()
    }

//    override fun computeScroll() {
//        super.computeScroll()
//        if (scroller.computeScrollOffset()) {
//            (parent as View).scrollTo(scroller.currX, scroller.currY)
//            //通过不断的重绘不断的调用computeScroll方法
//            invalidate()
//        }
//    }
}